package com.wh.wisdomsite.xbox.client.service.hkws;

import com.alibaba.fastjson.JSON;
import com.hikvision.cms.api.common.util.Digests;
import com.hikvision.cms.api.common.util.HttpClientSSLUtils;
import com.wh.wisdomsite.xbox.common.Constants;
import com.wh.wisdomsite.xbox.common.annotation.NeedInSessionType;
import com.wh.wisdomsite.xbox.common.annotation.ServiceMethod;
import com.wh.wisdomsite.xbox.common.annotation.ServiceProvider;
import com.wh.wisdomsite.xbox.common.reponse.CommonResult;
import com.wh.wisdomsite.xbox.common.util.XboxUtils;
import com.wh.wisdomsite.xbox.hkws.entity.*;
import com.wh.wisdomsite.xbox.pojo.hkws.tj.openapi.*;
import com.wh.wisdomsite.xbox.utils.Const;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import com.wh.wisdomsite.xbox.utils.json.JsonUtil_Google;
import com.wh.wisdomsite.xbox.utils.json.JsonUtil_Org;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;


/**
 * 天津 - 海康微视V2.8API调用
 * @author 谢泽鹏
 * @version V1.0
 * @Package com.wh.technology.xbox.client
 * @date 2014年10月8日 下午4:02:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 */
@ServiceProvider()
public class HkwsService_TJ {

    private static final Log logger = LogFactory.getLog(HkwsService_TJ.class);

    /**
     * 获取默认用户UUID
     * @param in
     * @return
     */
    @ServiceMethod(method = "hkws.tj.base.getDefaultUserUuid", title = "获取默认用户UUID", version = "1.0", needInSession = NeedInSessionType.NO)
    public CommonResult getDefaultUserUuid(DefaultUserUuidIn in){
        CommonResult commonResult = new CommonResult();
        try{
            logger.info("获取默认用户UUID->>hkws.tj.vss.getDefaultUserUuid->>开始");
            //获取URL
            String url = Const.HKWS_TJ_OPENAPI_HTTP_URL + Const.HKWS_TJ_INTERFACE_OPENAPI_SERVICE_BASE_GETDEFAULTUUID;
            //设置时间
            in.setTime(System.currentTimeMillis());
            //生成参数模型
            String params = JSON.toJSONString(in);
            //获取result
            String result = HttpClientSSLUtils.doPost(url + "?token=" + Digests.buildToken(url + "?" + params, null, Const.HKWS_TJ_OPENAPI_SECRET), params);
            //获取响应结果
            commonResult = XboxUtils.getOpenAPICommonResult(commonResult, result, DefaultUserUuidOut.class);
            Const.HKWS_TJ_DEFAULT_UUID = commonResult.getResult() + "";
            logger.info("获取默认用户UUID-->>hkws.tj.base.getDefaultUserUuid->>结束");
        }catch (Exception e){
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage(Constants.CODE_FAIL_MSG);
            in.getXboxRequestContext().setException(e);
            e.printStackTrace();
            logger.error("获取默认用户UUID->>hkws.tj.base.getDefaultUserUuid->>Exception " + e.getMessage());
        }
        return commonResult;
    }


    /**
     * 获取监控点
     * @param in
     * @return
     */
    @ServiceMethod(method = "hkws.tj.vss.getCameras", title = "获取监控点", version = "1.0", needInSession = NeedInSessionType.NO)
    public CommonResult getCameras(CamerasIn in){
        CommonResult commonResult = new CommonResult();
        try{
            logger.info("获取监控点->>hkws.tj.vss.getCameras->>开始");
            //获取URL
            String url = Const.HKWS_TJ_OPENAPI_HTTP_URL + Const.HKWS_TJ_INTERFACE_OPENAPI_SERVICE_VSS_RES_GETCAMERAS;
            //设置时间
            in.setTime(System.currentTimeMillis() + "");
            //生成参数模型
            String params = JSON.toJSONString(in);
            //获取result
            String result = HttpClientSSLUtils.doPost(url + "?token=" + Digests.buildToken(url + "?" + params, null, Const.HKWS_TJ_OPENAPI_SECRET), params);
            //获取响应结果
            commonResult = this.getCommonResult(commonResult, result);
            logger.info("获取监控点->>hkws.tj.vss.getCameras->>结束");
        }catch (Exception e){
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage(Constants.CODE_FAIL_MSG);
            in.getXboxRequestContext().setException(e);
            e.printStackTrace();
            logger.error("获取监控点->>hkws.tj.vss.getCameras->>Exception " + e.getMessage());
        }
        return commonResult;
    }


    /**
     * 根据监控点名称获取监控点
     * @param in
     * @return
     */
    @ServiceMethod(method = "hkws.tj.vss.getCamerasByName", title = "根据监控点名称获取监控点", version = "1.0", needInSession = NeedInSessionType.NO)
    public CommonResult getCamerasByName(CamerasByNameIn in){
        CommonResult commonResult = new CommonResult();
        try{
            logger.info("根据监控点名称获取监控点->>hkws.tj.vss.getCamerasByName->>开始");
            //获取URL
            String url = Const.HKWS_TJ_OPENAPI_HTTP_URL + Const.HKWS_TJ_INTERFACE_OPENAPI_SERVICE_VSS_RES_GETCAMERASBYNAME;
            //设置时间
            in.setTime(System.currentTimeMillis() + "");
            //生成参数模型
            String params = JSON.toJSONString(in);
            //获取result
            String result = HttpClientSSLUtils.doPost(url + "?token=" + Digests.buildToken(url + "?" + params, null, Const.HKWS_TJ_OPENAPI_SECRET), params);
            //获取响应结果
            commonResult = this.getCommonResult(commonResult, result);
            logger.info("根据监控点名称获取监控点->>hkws.tj.vss.getCamerasByName->>结束");
        }catch (Exception e){
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage(Constants.CODE_FAIL_MSG);
            in.getXboxRequestContext().setException(e);
            e.printStackTrace();
            logger.error("根据监控点名称获取监控点->>hkws.tj.vss.getCamerasByName->>Exception " + e.getMessage());
        }
        return commonResult;
    }


    /**
     * 获取CommonResult
     * @param commonResult 响应对象
     * @param result 响应报文
     * @return
     * @throws Exception
     */
    public CommonResult getCommonResult(CommonResult commonResult, String result) throws Exception{
        try{
            if(StringUtils.isEmpty(result)){
                commonResult.setCode(Constants.CODE_FAIL);
                commonResult.setMessage(Constants.RETURN_NETWORK_EXCEIPTION);
                return commonResult;
            }
            //创建jsonObject
            JSONObject jsonObject = new JSONObject(result);
            //获取返回编码
            String resCode = JsonUtil_Org.getValueByKey(jsonObject, Constants.ERRORCODE);
            //获取返回消息
            String resMsg = JsonUtil_Org.getValueByKey(jsonObject, Constants.ERRORMESSAGE);
            //成功
            if(Const.HKWS_SUCCESS_0.equalsIgnoreCase(resCode)){
                //获取返回结果
                String listCamerasOuts = JsonUtil_Org.getValueByKey(jsonObject, Constants.DATA);
                //转换成集合
                CamerasOut camerasOuts = (CamerasOut) JsonUtil_Google.getGsonObjectByObject(listCamerasOuts, CamerasOut.class);
                for(GetCameras getCameras  : camerasOuts.getList()) {
                    getCameras.setCameraUrl(Const.HKWS_TJ_URL + ":" + Const.HKWS_TJ_RES_PORT + "/mag/hls/" + getCameras.getCameraUuid() + "/1/live.m3u8");
                    getCameras.setCameraUrlProxy(Const.HKWS_TJ_PROXY_URL + ":" + Const.HKWS_TJ_RES_PROXY_PORT + "/mag/hls/" + getCameras.getCameraUuid() + "/1/live.m3u8");
                }
                commonResult.setResult(camerasOuts.getList());
            }else{
                commonResult.setCode(Constants.CODE_FAIL);
                commonResult.setMessage(resMsg);
            }
        } catch (Exception e){
            e.printStackTrace();
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage(Constants.CODE_FAIL_MSG);
            throw new RuntimeException(e.getMessage());
        }
        return commonResult;
    }


    /**
     * 获取编码设备
     * @param in
     * @return
     */
    @ServiceMethod(method = "hkws.tj.vss.getEncoderDevicesEx", title = "获取编码设备", version = "1.0", needInSession = NeedInSessionType.NO)
    public CommonResult getEncoderDevicesEx(EncoderDevicesExIn in){
        CommonResult commonResult = new CommonResult();
        try{
            logger.info("获取编码设备->>hkws.tj.vss.getEncoderDevicesEx->>开始");
            //获取URL
            String url = Const.HKWS_TJ_OPENAPI_HTTP_URL + Const.HKWS_TJ_INTERFACE_OPENAPI_SERVICE_VSS_RES_GETENCODERDEVICESEX;
            //设置时间
            in.setTime(System.currentTimeMillis() + "");
            //生成参数模型
            String params = JSON.toJSONString(in);
            //获取result
            String result = HttpClientSSLUtils.doPost(url + "?token=" + Digests.buildToken(url + "?" + params, null, Const.HKWS_TJ_OPENAPI_SECRET), params);
            //获取响应结果
            commonResult = XboxUtils.getOpenAPICommonResult(commonResult, result, EncoderDevicesExOut.class);
            logger.info("获取编码设备->>hkws.tj.vss.getEncoderDevicesEx->>结束");
        }catch (Exception e){
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage(Constants.CODE_FAIL_MSG);
            in.getXboxRequestContext().setException(e);
            e.printStackTrace();
            logger.error("获取编码设备->>hkws.tj.vss.getEncoderDevicesEx->>Exception " + e.getMessage());
        }
            return commonResult;
    }


    /**
     * 手动抓图
     * @param in
     * @return
     */
    @ServiceMethod(method = "hkws.tj.vss.blgCapturePic", title = "手动抓图", version = "1.0", needInSession = NeedInSessionType.NO)
    public CommonResult blgCapturePic(CapturePicIn in){
        CommonResult commonResult = new CommonResult();
        try{
            logger.info("手动抓图->>hkws.tj.vss.blgCapturePic->>开始");
            //获取URL
            String url = Const.HKWS_TJ_OPENAPI_HTTP_URL + Const.HKWS_TJ_INTERFACE_OPENAPI_SERVICE_VSS_PICTURE_BLGCAPTUREPIC;
            //设置时间
            in.setTime(System.currentTimeMillis() + "");
            //生成参数模型
            String params = JSON.toJSONString(in);
            //获取result
            String result = HttpClientSSLUtils.doPost(url + "?token=" + Digests.buildToken(url + "?" + params, null, Const.HKWS_TJ_OPENAPI_SECRET), params);
            //获取响应结果
            commonResult = XboxUtils.getOpenAPICommonResult(commonResult, result, CapturePicOut.class);
            logger.info("手动抓图->>hkws.tj.vss.blgCapturePic->>结束");
        }catch (Exception e){
            commonResult.setCode(Constants.CODE_FAIL);
            commonResult.setMessage(Constants.CODE_FAIL_MSG);
            in.getXboxRequestContext().setException(e);
            e.printStackTrace();
            logger.error("手动抓图->>hkws.tj.vss.blgCapturePic->>Exception " + e.getMessage());
        }
        return commonResult;
    }


}
